package com.fd.controller;

import com.fd.bean.Address;
import com.fd.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    /*@RequestMapping(value = "/login",method = RequestMethod.GET)
    public String hellworld(){
        System.out.println("进入登录页面");
        return "login";
    }*/
    /*@RequestMapping("/save")
    public String save(){
        System.out.println("保存成功!!!");
        return "login";
    }*/

    //接收form表单
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public  String login(String username,String password){
        System.out.println("成功接收到数据!!!");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return "index";
    }

    //正常情况下,方法中闯入的参数与前台的参数名一致
    /*@RequestMapping(value = "/ok",method = RequestMethod.POST)
    //Spring4.3之上才有的注解
    *//*@POSTRequstMappinig*//*
    public String ok(String username,String password){
        System.out.println("成功接受到数据!!!");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return "index";
    }*/

    //非正常情况下,方法中传入的参数与前台的参数名不一致
    /*@RequestMapping(value = "/ok",method = RequestMethod.POST)
    public String ok(@RequestParam(value = "username")String name,@RequestParam(value = "password") String pd){
        System.out.println("成功接受到数据!!!");
        System.out.println("username = " + name);
        System.out.println("password = " + pd);
        return "ok";
    }*/

    //3.使用一个对象去接受来自前台的参数
    /*@RequestMapping(value = "/ok", method = RequestMethod.POST)
    public String ok(User user) {
        System.out.println("成功接受到数据!!!");
        System.out.println("user = " + user);
        return "ok";
    }*/


    /*//3.使用一个集合去接受来自前台的参数
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public String ok(List<Map<String,Object>> user) {
        System.out.println("成功接受到数据!!!");
        System.out.println("user = " + user);
        return "login";
    }*/

    /*//跳转到 WEB-INF/user/form.jsp
    @RequestMapping("/save")
    public String from(){
        return "login";
    }

    //form表单提交数据到此处!获取在转发到success.jsp
    @RequestMapping("/user")
    public String add(User user){
        System.out.println(user);
        return "login";
    }

    @RequestMapping("/edit")
    public ModelAndView edit() {
        User user = new User();
        user.setUsername("用户名!");
        user.setAge(11);
        user.setPassword("123");

        Address address = new Address();

        address.setProvince("辽宁");
        address.setCity("葫芦岛");

        user.setAddress(address);

        ModelAndView modelAndView = new ModelAndView();
        //viewname 指定要查找的页面
        modelAndView.setViewName("/form");
        //添加modelAndView添加携带的user数据
        modelAndView.addObject("user", user);

        return modelAndView;
    }*/


    @RequestMapping("/map")
    public String retMap(Map<String, Object> map) {
        map.put("name", "map return");
        return "show";
    }

    @RequestMapping("/model")
    public String retModel(Model model) {
        model.addAttribute("name", "model return!");
        return "show";
    }

    @RequestMapping("/servlet")
    public String getSlt(HttpServletRequest request) {
        //TODO 如果需要引入原生api类!直接在参数引入即可!
        return "show";
    }

    @RequestMapping("/toUpload")
    public String toUpload(){
        //跳转到上传页面
        return  "io/upload";
    }

    @RequestMapping("/upload")
    public  String saveFile(@RequestParam("name") String name , @RequestParam("file") MultipartFile file) throws IOException {
        //接收表单提交的数据,包含文件
        System.out.println("name = " + name);
        if (!file.isEmpty())
        {
            file.transferTo(new File("C:/"+file.getOriginalFilename()));
        }
        return "success";
    }

}


